import baseAxios from '../../config/axios';

const state = {
    warnDetailShow: false,
    warnInfo: {
        infoList: []
    },
    pointDetailShow: false,
    pointInfo: {

    }
}

const getters = {
    warnDetailShow: state => state.warnDetailShow,
    warnInfo: state => state.warnInfo,
    pointDetailShow: state => state.pointDetailShow,
    pointInfo: state => state.pointInfo
}

const mutations = {
    warnDetailShow(state, flag) {
        state.warnDetailShow = flag;
    },
    setWarnInfoList(state, list) {
        state.warnInfo.infoList = list;
    },
    pointDetailShow(state, flag) {
        state.pointDetailShow = flag;
    },
    setPointInfo(state, pointInfo) {
        state.pointInfo = pointInfo;
    }
}

const actions = {
    warnDetailShow(context, flag) {
        context.commit('warnDetailShow', !!flag);
    },
    showWarnDetail(context, item) {
        console.log(item);
        baseAxios.getUrl('pubinfo-restful/tdIotDevice/queryDevDetailByDeviceId.rf', {
            params: {
                deviceType: item.deviceType,
                deviceId: item.deviceId
            }
        }).then(data => {
            let list = getWarnInfoList(data.data);
            context.commit('setWarnInfoList', list);
            context.dispatch('warnDetailShow', true);
        });
    },
    pointDetailShow(context, flag) {
        context.commit('pointDetailShow', !!flag);
    },
    getPointInfo(context, item) {
        context.dispatch('pointDetailShow', true);
    }
}

export default {
    state,
    getters,
    actions,
    mutations
}

function getWarnInfoList(data) {
    let arr = [
        c('设备大类', data.device_big_name),
        c('设备类型', data.device_name),
        c('设备名称', data.monitor_name),
        c('发生时间', data.data_time.replace(/:\d{3}$/, '')),
        c('所属机构', data.company_name),
        c('地址', data.company_address)
    ];
    return arr;
}

function c(name, value) {
    return {
        name,
        value
    }
}
